//
//  UserInfo.swift
//  ParkingLot
//
//  Created by lxl on 2018/6/30.
//  Copyright © 2018年 lxl. All rights reserved.
//

import UIKit
let PLUserDefult = UserDefaults.standard
let PLUserInfo = "PLUserInfo"
let SUserToken = "userToken"
let ReviewVersion = "ReviewVersion"
let Longitude = "Longitude"
let Latitude = "Latitude"

class UserInfo: NSObject {
 
    class func getTheUserToken() -> String{
//        return "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1laWRlbnRpZmllciI6IjIiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoiYWRtaW4iLCJBc3BOZXQuSWRlbnRpdHkuU2VjdXJpdHlTdGFtcCI6ImJmYjYxMDMxLWRlMjctZDk1MS02MzE4LTM5ZWYyZjUzYmJlNyIsImh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vd3MvMjAwOC8wNi9pZGVudGl0eS9jbGFpbXMvcm9sZSI6ImFkbWluIiwiaHR0cDovL3d3dy5hc3BuZXRib2lsZXJwbGF0ZS5jb20vaWRlbnRpdHkvY2xhaW1zL3RlbmFudElkIjoiMSIsInN1YiI6IjIiLCJqdGkiOiI5ZjI5YjZhNi1hMjQyLTQyZDktYTVmNi0wNjliZWIwNjExZmIiLCJpYXQiOjE1NjU5MTg4MjksInRva2VuX3ZhbGlkaXR5X2tleSI6IjE2MzcyNWVlLWJmZjYtNGI2Ni05NThjLWI3YTVhYjA4MThiYSIsInVzZXJfaWRlbnRpZmllciI6IjJAMSIsIm5iZiI6MTU2NTkxODgyOSwiZXhwIjoxNTY3MTI4NDI5LCJpc3MiOiJDc3Byb2pCdWlsZGVyIiwiYXVkIjoiQ3Nwcm9qQnVpbGRlciJ9.H2xhXjXqtlfyKcyEdJXXAdbP4CCzfPNTc-Nuv797v-s"
//        return "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1laWRlbnRpZmllciI6IjMiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoidV82MzcwMDY5ODQyMjkxOTA5NzQiLCJBc3BOZXQuSWRlbnRpdHkuU2VjdXJpdHlTdGFtcCI6IlZGWk1CNkxaNTQ1UUxIWVBFNlpVVzVFSEM2T1JMRE9NIiwiaHR0cDovL3NjaGVtYXMubWljcm9zb2Z0LmNvbS93cy8yMDA4LzA2L2lkZW50aXR5L2NsYWltcy9yb2xlIjoidXNlciIsImh0dHA6Ly93d3cuYXNwbmV0Ym9pbGVycGxhdGUuY29tL2lkZW50aXR5L2NsYWltcy90ZW5hbnRJZCI6IjEiLCJzdWIiOiIzIiwianRpIjoiNDM1N2VhOWEtYjhjOC00ZGU1LTliOGUtNmQyNGUwOTllYzljIiwiaWF0IjoxNTY1NTcwODg2LCJ0b2tlbl92YWxpZGl0eV9rZXkiOiI5OTY5YWNkZC00MTlmLTQ1ODMtOWNkYi00M2FlNTBmNGU4MzIiLCJ1c2VyX2lkZW50aWZpZXIiOiIzQDEiLCJuYmYiOjE1NjU1NzA4ODYsImV4cCI6MTU2Njc4MDQ4NiwiaXNzIjoiQ3Nwcm9qQnVpbGRlciIsImF1ZCI6IkNzcHJvakJ1aWxkZXIifQ.JV1tHM-J4uQOQW2lvhs-6UBatCD79OE_Q-ez2xQIvwQ"
        if let token = PLUserDefult.object(forKey: SUserToken){
            return token as! String
        }
        return ""
    }
    class func getUserIsLogged() -> Bool{
        if getTheUserToken().count > 0{
            return true
        }
       return false
    }
    class func getLoggedUserid() -> Int{
        if let dict = PLUserDefult.object(forKey: PLUserInfo){
            let userInfo = dict as! [String : Any]
            if let userId = userInfo["userId"]{
                return userId as! Int
            }
        }
        return 0
    }
    class func saveUserInfo(dict:[String : Any]) -> Void{
        PLUserDefult.set(dict, forKey: PLUserInfo)
        PLUserDefult.synchronize()
        
//        UserDefaults.standard.set(accToken, forKey: SUserToken)

    }
    
    class func userLogOut(){
//        PLUserDefult.removeObject(forKey: PLUserInfo)
        PLUserDefult.removeObject(forKey: SUserToken)

        PLUserDefult.synchronize()

    }
    class func getUserMobile()  -> String{
        
        let mobile = getLoggedUserInfoWith(key: "mobile")
        if mobile is String{
            return mobile as! String
        }
        return ""
    }
    class func getLoggedUserInfoWith(key:String) -> Any{
        if let dict = PLUserDefult.object(forKey: PLUserInfo){
            let userInfo = dict as! [String : Any]
            if let value = userInfo[key]{
                return value
            }
        }
        return ""
    }
    class func getLoggedUserName() -> String{
        var realName = UserInfo.getLoggedUserInfoWith(key: "realname") as! String
        if realName.count <= 0{
            if let name = PLUserDefult.object(forKey: "realname"){
                realName = name as! String
                if realName.count > 0{
                    return realName
                }
            }
//            return realName
        }
        return realName

    }
    class func getLoggedUserCode() -> String{
        var userName = ""
        let name = UserInfo.getLoggedUserInfoWith(key: "nickname") as! String
        let userid = UserInfo.getLoggedUserid()
        let mobile = UserInfo.getUserMobile()
        if name.count > 1{
            userName = name
        }else if  mobile.count > 1{
            userName = mobile
        }else{
            userName = String(userid)
        }
        return userName
    }
    
}
